package com.example.animations.controls;

import android.animation.Animator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.animations.R;

public class TestView extends RelativeLayout implements Animator.AnimatorListener {
    private boolean _isLarge = true;
    private float _endRadius = 0f;

    public TestView(Context context) {
        super(context);
        init();
    }

    public TestView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TestView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        _endRadius = (float) Math.sqrt((Math.pow(r, 2) + Math.pow(b, 2)));
    }

    private void init() {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.test_view, this, true);
    }

    public void startAnim(long millis) {
        Animator animator = ViewAnimationUtils.createCircularReveal(this, 0, 0, _isLarge ? _endRadius : 0f, _isLarge ? 0f : _endRadius);
        animator.addListener(this);
        animator.setDuration(millis);
        animator.start();
        _isLarge = !_isLarge;
    }

    @Override
    public void onAnimationStart(@NonNull Animator animation) {
        setVisibility(View.VISIBLE);
    }

    @Override
    public void onAnimationEnd(@NonNull Animator animation) {
        if (!_isLarge) {
            setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void onAnimationCancel(@NonNull Animator animation) {

    }

    @Override
    public void onAnimationRepeat(@NonNull Animator animation) {

    }
}
